*Replication Paper
* Dancygier, Rafaela M. and Michael D. Donnelly. 2013. 
* "Sectoral Economies, Economic Contexts, and Attitudes toward Immigration."
* Journal of Politics, Vol. 75, No. 1, January 2013, 17-35

* Elizabeth Dekeyser and Michael Freedman
 

set more off

* Prepare elections data for merge

use "C:\Users\Freedman\Dropbox\Gary King Project\elections2.dta", clear
rename cnty cntry
gen pres1  = date(presidential, "MDY")
gen pres2 = date(presii, "MDY")
gen pres3 = date(presiii, "MDY")
gen parl1 = date(parli1, "MDY")
gen parl2 = date(parli2, "MDY")
gen parl3 = date(parli3, "MDY")
gen parl4 = date(parli4, "MDY")
format pres1 pres2 pres3 parl1 parl2 parl3 parl4 %td
keep cntry pres1 pres2 pres3 parl1 parl2 parl3 parl4
sort cntry
save "C:\Users\Freedman\Dropbox\Gary King Project\elections.fixed.dta", replace 

* Prepare country data for merge
use "C:\Users\Freedman\Dropbox\Gary King Project\ESSMDCNTRYe2_F1.dta", clear
keep cntry c_gdppc_*
sort cntry
save "C:\Users\Freedman\Dropbox\Gary King Project\country.dta", replace

* Take out ESS variables 
use "C:\Users\Freedman\Dropbox\MIT courses\First Semester\Jens and Hidalgo\research paper\datafiles\ESS1-5_cumulative_e01_1.dta", clear
keep  cntry essround idno inwdd inwdds inwdde inwmm inwmms inwmme inwyr inwyys inwyye inwshh inwsmm inwehh inwemm inwtm supqdd supqmm supqyr im*  stfeco polintr lrscale tvtot tvpol rdtot rdpol nwsptot nwsppol vote pweight
sort cntry
merge cntry using "C:\Users\Freedman\Dropbox\Gary King Project\country.dta"
tab _merge
drop if _merge == 2
drop _merge
sort cntry
merge cntry using "C:\Users\Freedman\Dropbox\Gary King Project\elections.fixed.dta"
tab _merge
drop if _merge==2
drop _merge
egen year =  rowtotal(inwyr inwyys)
egen month =  rowtotal(inwmm inwmms)
egen day  =  rowtotal(inwdd inwdds)

replace year = . if year==0
replace month=. if month==0
replace day = . if day==0

gen edate = mdy(month, day, year)
format edate %td
bysort year: sum imsmetn imdfetn impcntr
keep essround idno cntry year month day edate /*c_gdppc_* */ im* pres1 pres2 pres3 parl1 parl2 parl3 parl4 stfeco polintr lrscale tvtot tvpol rdtot rdpol nwsptot nwsppol vote pweight
sort cntry
drop  imprich impsafe impdiff impfree impenv imptrad impfun

* Country Labels
gen ctry=.
replace ctry=1 if cntry=="AT"
replace ctry=2 if cntry=="BE"
replace ctry=3 if cntry=="BG"
replace ctry=4 if cntry=="CH"
replace ctry=5 if cntry=="CY"
replace ctry=6 if cntry=="CZ"
replace ctry=7 if cntry=="DE"
replace ctry=8 if cntry=="DK"
replace ctry=9 if cntry=="EE"
replace ctry=10 if cntry=="ES"
replace ctry=11 if cntry=="FI"
replace ctry=12 if cntry=="FR"
replace ctry=13 if cntry=="GB"
replace ctry=14 if cntry=="GR"
replace ctry=15 if cntry=="HU"
replace ctry=16 if cntry=="IE"
replace ctry=17 if cntry=="IL"
replace ctry=18 if cntry=="IT"
replace ctry=19 if cntry=="LU"
replace ctry=20 if cntry=="NL"
replace ctry=21 if cntry=="NO"
replace ctry=22 if cntry=="PL"
replace ctry=23 if cntry=="PT"
replace ctry=24 if cntry=="RU"
replace ctry=25 if cntry=="SE"
replace ctry=26 if cntry=="SI"
replace ctry=27 if cntry=="SK"
replace ctry=28 if cntry=="TR"
replace ctry=29 if cntry=="UA"
sort essround idno ctry
save "C:\Users\Freedman\Dropbox\Gary King Project\interview.dta", replace

* Prepare Dancygier data for merge
use "C:\Users\Freedman\Dropbox\Gary King Project\ESS1_4_JOP.dta", clear
drop if essround==.
drop if idno==.
sort essround idno ctry
merge essround idno ctry using "C:\Users\Freedman\Dropbox\Gary King Project\interview.dta"
drop if _merge==2 & essround==4
drop _merge

* Generate distance variable

gen diffpres1= abs(edate- pres1)
gen diffpres2= abs(edate- pres2)
gen diffpres3= abs(edate- pres3)
gen diffparl1= abs(edate- parl1)
gen diffparl2= abs(edate- parl2)
gen diffparl3= abs(edate- parl3)
gen diffparl4= abs(edate- parl4)

egen proximity=rowmin( diffpres1 diffpres2 diffpres3 diffparl1 diffparl2 diffparl3 diffparl4)
gen proximity2= log(proximity)

xi i.essround i.year i.ctry 


gen ess2Xproximity= _Iessround_2*proximity
gen ess3Xproximity= _Iessround_3*proximity
gen ess4Xproximity= _Iessround_4*proximity
gen ess5Xproximity= _Iessround_5*proximity


gen year2003Xproximity= _Iyear_2003*proximity
gen year2004Xproximity= _Iyear_2004*proximity
gen year2005Xproximity= _Iyear_2005*proximity
gen year2006Xproximity= _Iyear_2006*proximity
gen year2007Xproximity= _Iyear_2007*proximity
gen year2008Xproximity= _Iyear_2008*proximity
gen year2009Xproximity= _Iyear_2009*proximity



gen ess2Xproximity2= _Iessround_2*proximity2
gen ess3Xproximity2= _Iessround_3*proximity2
gen ess4Xproximity2= _Iessround_4*proximity2
gen ess5Xproximity2= _Iessround_5*proximity2


gen year2003Xproximity2= _Iyear_2003*proximity2
gen year2004Xproximity2= _Iyear_2004*proximity2
gen year2005Xproximity2= _Iyear_2005*proximity2
gen year2006Xproximity2= _Iyear_2006*proximity2
gen year2007Xproximity2= _Iyear_2007*proximity2
gen year2008Xproximity2= _Iyear_2008*proximity2
gen year2009Xproximity2= _Iyear_2009*proximity2
gen year2010Xproximity2= _Iyear_2010*proximity2
gen year2011Xproximity2= _Iyear_2011*proximity2
gen year2012Xproximity2= _Iyear_2012*proximity2

gen countryXyear=ctry*year
gen countryXess=ctry*essround

gen country=cntry
replace	country="Austria"	if	country==	"AT"
replace	country="Belgium"	if	country==	"BE"
replace	country="Bulgaria" if	country==	"BG"
replace	country="Switzerland" if	country==	"CH"
replace	country="Cyprus"	if	country==	"CY"
replace	country="Czech Republic"	if	country==	"CZ"
replace	country="Germany"	if	country==	"DE"
replace	country="Denmark" if	country==	"DK"
replace	country="Estonia"	if	country==	"EE"
replace	country="Spain"	if	country==	"ES"
replace	country="Finland"	if	country==	"FI"
replace	country="France"	if	country==	"FR"
replace	country="United Kingdom"	if	country==	"GB"
replace	country="Greece"	if	country==	"GR"
replace	country="Croatia"	if	country==	"HR"
replace	country="Hungary"	if	country==	"HU"
replace	country="Ireland"	if	country==	"IE"
replace	country="Israel"	if	country==	"IL"
replace	country="Iceland"	if	country==	"IS"
replace	country="Italy"	if	country==	"IT"
replace	country="Luxembourg"	if	country==	"LU"
replace	country="Latvia"	if	country==	"LV"
replace	country="Netherlands"	if	country==	"NL"
replace	country="Norway"	if	country==	"NO"
replace	country="Poland"	if	country==	"PL"
replace	country="Portugal"	if	country==	"PT"
replace	country="Romania"	if	country==	"RO"
replace	country="Russian Federation"	if	country==	"RU"
replace	country="Sweden"	if	country==	"SE"
replace	country="Slovenia"	if	country==	"SI"
replace	country="Slovak Republic"	if	country==	"SK"
replace	country="Turkey"	if	country==	"TR"
replace	country="Ukraine"	if	country==	"UA"

drop if cntry == "IL" | cntry =="TR" | cntry == "UA" | cntry == "RU"
recode imsmetn (4=1) (3=2) (2=3) (1=4), gen (imoe)

recode impcntr (4=1) (3=2) (2=3) (1=4), gen (imop_new)

gen proxXtvtot=proximity2*tvtot
gen proxXtvpol=proximity2*tvpol
gen proxXrdtot=proximity2*rdtot
gen proxXrdpol=proximity2*rdpol
gen proxXnwsptot=proximity2*nwsptot
gen proxXnwsppol=proximity2*nwsppol
gen proxXpolintr=proximity2*polintr
gen proxXvote=proximity2*vote
gen proxXlrscale=proximity2*lrscale
gen proxXeco=proximity2*stfeco
save "C:\Users\Freedman\Dropbox\Gary King Project\data_final.dta", replace


use "C:\Users\Freedman\Dropbox\Gary King Project\data_final.dta", replace
 
* Table 1

eststo clear


oprobit imop proximity2 isced3 isced4 isced5 age male union i.essround imchg	empchg  i.ctry ///
[pw=dweight]  if hiallsample==1 & native==1 & retired==0 , vce(cluster  countryXess)
eststo T2_1

oprobit imop proximity2  isced3 isced4 isced5 age male union i.essround i.ctry ///
  ess2Xproximity2 ess3Xproximity2 ess4Xproximity2 imchg empchg ///
[pw=dweight]  if  hiallsample==1 & native==1 & retired==0 , vce(cluster  countryXess)
eststo T2_2

oprobit imop 	proximity2	isced3 isced4 isced5 age male union i.essround i.ctry ///
ess2Xproximity2 ess3Xproximity2 ess4Xproximity2 empchg  ///
urate  gdppercapitaconstant2000us pcforborn benefits ///
[pw=dweight]  if  hiallsample==1 & native==1 & retired==0 , vce(cluster  countryXess)
eststo T2_3

esttab using "C:\Users\Freedman\Desktop\LaTeX workshop\table interaction new.tex", replace  se  star( * 0.10 ** 0.05 *** 0.01 )  pr2 ///
drop( *.ctry isced3 isced4 isced5 age male union )

* Table 2
eststo clear

* Own Ethnic Group
oprobit imoe proximity2 imchg	empchg isced3 isced4 isced5 age male union i.essround i.ctry ///
[pw=dweight] if hiallsample==1 &  native==1 & retired==0  , vce(cluster  countryXess)
eststo T3_1

oprobit imoe 	proximity2	empchg isced3 isced4 isced5 age male union i.essround i.ctry ///
ess2Xproximity2 ess3Xproximity2 ess4Xproximity2 ///
urate  gdppercapitaconstant2000us pcforborn benefits ///
[pw=dweight]  if hiallsample==1 &  native==1 & retired==0  , vce(cluster  countryXess)
eststo T3_3

recode imdfetn (4=1) (3=2) (2=3) (1=4), gen (imod)

* Different ethnic group
oprobit imod proximity2 imchg	empchg isced3 isced4 isced5 age male union i.essround i.ctry ///
[pw=dweight]  if hiallsample==1 &  native==1 & retired==0  , vce(cluster  countryXess)
eststo T3_4

oprobit imod 	proximity2	empchg isced3 isced4 isced5 age male union i.essround i.ctry ///
ess2Xproximity2 ess3Xproximity2 ess4Xproximity2 ///
urate  gdppercapitaconstant2000us pcforborn benefits ///
[pw=dweight]  if hiallsample==1 &  native==1 & retired==0  , vce(cluster  countryXess)
eststo T3_6

esttab using "C:\Users\Freedman\Desktop\LaTeX workshop\table other.tex", replace  se  star( * 0.10 ** 0.05 *** 0.01 )  pr2 ///
drop( *.ctry imchg	empchg isced3 isced4 isced5 age male union )

* Appendix Table 1
eststo clear

oprobit imop proximity2 isced3 isced4 isced5 age male union i.essround imchg	empchg  i.ctry ///
[pw=dweight]  if native==1 & retired==0 , vce(cluster  countryXess)
eststo T4_1

oprobit imop proximity2  isced3 isced4 isced5 age male union i.essround i.ctry ///
  ess2Xproximity2 ess3Xproximity2 ess4Xproximity2 imchg empchg ///
[pw=dweight]  if  native==1 & retired==0 , vce(cluster  countryXess)
eststo T4_2

oprobit imop 	proximity2	isced3 isced4 isced5 age male union i.essround i.ctry ///
ess2Xproximity2 ess3Xproximity2 ess4Xproximity2 empchg  ///
urate  gdppercapitaconstant2000us pcforborn benefits ///
[pw=dweight]  if  native==1 & retired==0 , vce(cluster  countryXess)
eststo T4_3

esttab using "C:\Users\Freedman\Desktop\LaTeX workshop\table appendix new.tex", replace  se  star( * 0.10 ** 0.05 *** 0.01 )  pr2 ///
drop( *.ctry isced3 isced4 isced5 age male union )

* Appendix Table 2
eststo clear

global sample2 "Austria Belgium Bulgaria Croatia Cyprus Czech Denmark Estonia Finland France Germany Greece Hungary Ireland Italy Luxembourg Netherlands Norway Poland Portugal Slovak Slovenia Spain Sweden Switzerland United Kingdom"

foreach y of global sample2 {
                oprobit imop ess4Xproximity2 proximity2	isced3 isced4 isced5 age male union i.essround i.ctry ///
ess2Xproximity2 ess3Xproximity2  empchg /// 
urate  gdppercapitaconstant2000us pcforborn benefits  ///
[pw=dweight]  if  native==1 & retired==0 & country!="`y'" , vce(cluster  countryXess)
       eststo T_`y'
	   }
esttab using "C:\Users\Freedman\Desktop\LaTeX workshop\table countries new.tex", replace  se  star( * 0.10 ** 0.05 *** 0.01 )  pr2 ///
	   drop( *.ctry isced3 isced4 isced5 age male union *.essround ess2Xproximity2 ess3Xproximity2  empchg urate  gdppercapitaconstant2000us pcforborn benefits )


